<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class AuthorityGroup extends Base
{
    /**
     * 权限套餐列表.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function index()
    {
        $meal = model('authority_group')->getMealAll();
        return $this->fetch('',['meal'=>$meal]);
    }

    /**
     * 添加权限列表
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function meal_add()
    {
        if(\request()->isPost()){
            $name = input('post.name','','trim');
            $menus = input('post.menus/a');
            $data = [
                'name' => $name,
                'menus' => $menus
            ];
            if(empty($name)){
                $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'套餐名称不能为空');
            }
            if(model('authority_group')->get(['name'=>$name])){
                $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'套餐已经存在');
            }
            if(empty($menus)){
                $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'请选择权限');
            }
            $data['menus'] = implode(',',$data['menus']);
            $id = model('authority_group')->add($data);
            if($id){
                $this->result($_SERVER['HTTP_REFERER'],config('code.success'),'添加成功');
            }else{
                $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'添加失败');
            }
        }else{
            return $this->fetch();
        }
    }

    /**
     * 修改权限套餐
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function authority_group_edit()
    {
        $id = input('param.id');
        if(\request()->isPost()){
            $name = input('post.name','','trim');
            $menus = input('post.menus/a');
            $data = [
                'name' => $name,
                'menus' => $menus[0]
            ];
            if(empty($name)){
                $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'套餐名称不能为空');
            }
            if(empty($menus)){
                $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'请选择权限');
            }
            $id = model('authority_group')->save($data,['id'=>$id]);
            if($id){
                $this->result($_SERVER['HTTP_REFERER'],config('code.success'),'修改成功');
            }else{
                $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'修改失败');
            }
        }else{
            $group = model('AuthorityGroup')->getMealAll($id);
            return $this->fetch('',['group'=>$group]);
        }
    }


    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
